<!-- 管网台账 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="管网编号" prop="pipeNetworkCode">
        <el-input
          v-model="queryParams.pipeNetworkCode"
          placeholder="请输入管网编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="pressureRating">
        <el-select
          v-model="queryParams.pressureRating"
          placeholder="请选择类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.pressure_rating"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班组" prop="teamId">
        <el-select
          v-model="queryParams.teamId"
          placeholder="请选择班组"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_person_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人员" prop="personId">
        <el-select
          v-model="queryParams.personId"
          placeholder="请选择负责人员"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_person_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="运行日期" prop="runDate">
        <el-date-picker
          clearable
          v-model="queryParams.runDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运行日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:network:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:network:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:network:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:network:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="networkList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        width="100"
        align="center"
        v-if="getColumn('index')"
      >
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="主键" align="center" prop="id" v-if="getColumn('id')" /> -->
      <el-table-column
        label="管网编号"
        align="center"
        prop="pipeNetworkCode"
        v-if="getColumn('pipeNetworkCode')"
      />
      <el-table-column
        label="压力等级"
        align="center"
        prop="pressureRating"
        v-if="getColumn('pressureRating')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.pressure_rating"
            :value="scope.row.pressureRating"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="管网长度(m)"
        align="center"
        prop="length"
        v-if="getColumn('length')"
      />
      <el-table-column
        label="所属街道"
        align="center"
        prop="streetId"
        v-if="getColumn('streetId')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_neighborhoodledger"
            :value="scope.row.streetId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="班组"
        align="center"
        prop="teamId"
        v-if="getColumn('teamId')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_person_group"
            :value="scope.row.teamId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="负责人员"
        align="center"
        prop="personId"
        v-if="getColumn('personId')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_person_name"
            :value="scope.row.personId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="运行日期"
        align="center"
        prop="runDate"
        width="180"
        v-if="getColumn('runDate')"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.runDate, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="getColumn('operate')"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:network:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:network:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :pageSizes="pageSizes"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改管网台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="管网编号" prop="pipeNetworkCode">
          <el-input
            v-model="form.pipeNetworkCode"
            placeholder="请输入管网编号"
          />
        </el-form-item>
        <el-form-item label="管网等级" prop="pressureRating">
          <el-select v-model="form.pressureRating" placeholder="请选择管网等级">
            <el-option
              v-for="dict in dict.type.pressure_rating"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管网长度(m)" prop="length">
          <el-input v-model="form.length" placeholder="请输入管网长度(m)" />
        </el-form-item>
        <el-form-item label="所属小区" prop="neighborId">
          <el-select v-model="form.neighborId" placeholder="请选择所属小区">
            <el-option
              v-for="dict in dict.type.bus_neighborhood"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属街道" prop="streetId">
          <el-select v-model="form.streetId" placeholder="请选择所属街道">
            <el-option
              v-for="dict in dict.type.bus_neighborhoodledger"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="班组" prop="teamId">
          <el-select v-model="form.teamId" placeholder="请选择班组" @change="getStaffList">
            <el-option
              v-for="dict in dict.type.bus_person_group"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人员" prop="personId">
          <el-select v-model="form.personId" placeholder="请选择负责人员">
            <el-option
              v-for="item in staffList"
              :key="item.id"
              :label="item.personName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="施工方" prop="constructorId">
          <el-select v-model="form.constructorId" placeholder="请选择施工方">
            <el-option
              v-for="dict in dict.type.constructor_id"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="材质" prop="materialType">
          <el-select v-model="form.materialType" placeholder="请选择材质">
            <el-option
              v-for="dict in dict.type.material_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运行日期" prop="runDate">
          <el-date-picker
            v-model="form.runDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择运行日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNetwork,
  getNetwork,
  delNetwork,
  addNetwork,
  updateNetwork,
} from "@/api/business/network";
import { getTeamPerson } from "@/api/business/person";
export default {
  name: "Network",
  dicts: [
    "bus_person_name",
    "constructor_id",
    "material_type",
    "bus_neighborhoodledger",
    "bus_person_group",
    "pressure_rating",
    "bus_neighborhood",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管网台账表格数据
      networkList: [],
      // 对应班组的所有员工
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pipeNetworkCode: null,
        teamId: null,
        personId: null,
        runDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pipeNetworkCode: [
          { required: true, message: "管网编号不能为空", trigger: "blur" },
        ],
        pressureRating: [
          { required: true, message: "压力等级不能为空", trigger: "change" },
        ],
        streetId: [
          { required: true, message: "所属街道不能为空", trigger: "change" },
        ],
        neighborId: [
          { required: true, message: "所属小区不能为空", trigger: "change" },
        ],
        teamId: [
          { required: true, message: "班组不能为空", trigger: "change" },
        ],
        personId: [
          { required: true, message: "负责人员不能为空", trigger: "change" },
        ],
      },
      // 列信息
      columns: [
        { key: "index", label: `序号`, visible: true },
        { key: "id", label: "主键", visible: true },
        { key: "pipeNetworkCode", label: "管网编号", visible: true },
        { key: "pressureRating", label: "压力等级", visible: true },
        { key: "length", label: "管网长度(m)", visible: true },
        { key: "streetId", label: "所属街道", visible: true },
        { key: "teamId", label: "班组", visible: true },
        // { key: "personId", label: "负责人员", visible: true },
        { key: "runDate", label: "运行日期", visible: true },
        { key: "operate", label: `操作`, visible: true },
      ],
      // 分页参数
      pageSizes: [10, 50, 100, 500, 1000],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询管网台账列表 */
    getList() {
      this.loading = true;
      listNetwork(this.queryParams).then((response) => {
        this.networkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取对应班组的员工
    getStaffList() {
      let teamId = this.form.teamId;
      console.log("teamId:", teamId);
       getTeamPerson(teamId).then((response) => {
        // console.log("response:", response);
        this.staffList = response.data;
      console.log("staffList:", this.staffList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createUser: null,
        createBy: null,
        createTime: null,
        updateUser: null,
        updateBy: null,
        updateTime: null,
        pipeNetworkCode: null,
        pressureRating: null,
        length: null,
        streetId: null,
        neighborId: null,
        address: null,
        longitude: null,
        latitude: null,
        teamId: null,
        personId: null,
        constructorId: null,
        materialType: null,
        runDate: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加管网台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getNetwork(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改管网台账";
        this.getStaffList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log("提交参数：");
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateNetwork(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNetwork(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除选中的数据项？")
        .then(function () {
          return delNetwork(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/network/export",
        {
          ...this.queryParams,
        },
        `network_${new Date().getTime()}.xlsx`
      );
    },
    /** 列是否显示 */
    getColumn(key) {
      const res = this.columns.filter((item) => item.key === key);
      if (res.length >= 1) {
        return res[0].visible;
      } else {
        return false;
      }
    },
  },
};
</script>
